/*
 * 用ViewFlipper中装载一张图片显示淡入淡出效果，
 * TimerThread 用来控制动画显示完(也就是n秒后)给Handler发送消息，
 * 并且进行本地、网络的一些信息验证工作
 * Handler 根据消息在Acvitity中调用不同的方法，对显示进行控制
 */
package com.android.sq.zouqi.login;


import android.app.Activity;
import android.app.ProgressDialog;
import android.content.Context;
import android.content.Intent;
import android.os.Bundle;
import android.os.Handler;
import android.os.Handler.Callback;
import android.os.Message;
import android.util.Log;
import android.view.MotionEvent;
import android.view.View;
import android.view.View.OnTouchListener;
import android.widget.ImageView;
import android.widget.Toast;

import com.android.sq.zouqi.Constants;
import com.android.sq.zouqi.R;
import com.android.sq.zouqi.main.mainActivity;
import com.android.sq.zouqi.main.pScrMain;
import com.android.sq.zouqi.pushservice.PushService;
import com.android.sq.zouqi.user.pCtrlUserInfo;

public class pScrWelcome_ImageView extends Activity implements Callback, OnTouchListener {
	private final static String TAG = "pScrWelcome";
	private Context context;
	private ImageView image_view;
	private int alpha = 0;
	private HandlerThread_ImageView handler_thread;
	//private int current_view_id;
	private int current_image_res_id;
	public pCtrlUserInfo ctrl_user_info;
	
	private ProgressDialog pro_dialog;
	public Handler my_handler = new Handler(this);
	
	// 网络登录和注册的线程
	public LoginThread loginThread;
	private int per_alpha = 6;	//每次渐变的alpha值
	
	@Override
	public void onCreate(Bundle savedInstanceState) {
		super.onCreate(savedInstanceState);
		setContentView(R.layout.main_imageview);
		context = this.getApplicationContext();
		
		ctrl_user_info = new pCtrlUserInfo(this, my_handler);
		image_view = (ImageView) findViewById(R.id.logo_imageview);
		handler_thread = new HandlerThread_ImageView(my_handler);
		
		pShowAppLogo();
		
	}
	
	// 显示app的logo - pCB01F01
	public void pShowAppLogo() {
		Log.e("BA", "Application Logo");
		
		image_view.setAlpha(0);
		current_image_res_id = R.drawable.applogo;
		//image_view.setImageResource(R.drawable.applogo);
		handler_thread.start();
	}
	
	// 显示公司的logo - pCB01F02
	public void pShowCompLogo() {
		Log.e("BA", "Company Logo");
		//this.setContentView(R.layout.main_anim_complogo);
		current_image_res_id = R.drawable.complogo;
		image_view.setAlpha(0);
		image_view.setImageResource(R.drawable.complogo);
		
		
		// 重新设置handler_thread的startTime，让动画再现一次
		handler_thread.setStartTime(System.currentTimeMillis());
		// 开启下一次动画周期
		handler_thread.setIsAnim(true);
		handler_thread.setAnimCount(0);
		
		// 开一个线程进行pLogin验证，这是一个网络交互
		loginThread = new LoginThread(this);
		loginThread.start();
				
		/*pro_dialog = ProgressDialog.show(pScrWelcome_ImageView.this, "连接中..",
				"登录中..请稍后....", true, true);*/
	}
	
	@Override
	public boolean handleMessage(Message msg) {
		// TODO Auto-generated method stub
		switch(msg.what) {
		case Constants.ANIMATION_TIME_UP :
			if (current_image_res_id == R.drawable.applogo) {
				//Log.e("BA", "app logo stopping flipping");
				pShowCompLogo();
			} else if (current_image_res_id == R.drawable.complogo) {
				// complogo动画停止，关闭handler_thread运行
				handler_thread.setIsRun(false);
				//Log.e("BA", "HanderThread is stopping running");
			}
			break;
		case Constants.FINISH_LOGIN :
			Log.e("BA", "Finish_Login");
			pGotoMain();
			break;
		case Constants.CHECK_NUM_ERROR :
			if (pro_dialog != null) {
				pro_dialog.dismiss();
			}
			Toast.makeText(pScrWelcome_ImageView.this, "ID验证失败:\n1.请检查您的网络连接.", //\n2.请联系我们.!",
					Toast.LENGTH_SHORT).show();
			break;
		case Constants.AUTO_REGIST_ERROR :
			if (pro_dialog != null) {
				pro_dialog.dismiss();
			}
			Toast.makeText(pScrWelcome_ImageView.this, "ID自动注册失败:\n1.请检查您的网络连接.", 
					Toast.LENGTH_SHORT).show();
			break;
		case Constants.ANIMATION_FADE_IN:
			fadeIn();
			//Log.e("BA", "HANDLER MSG, ANIM_COUNT = " + anim_count++);
			break;
		case Constants.ANIMATION_FADE_OUT:
			fadeOut();
			break;
		}
		return false;
	}
	private boolean isFinishLogin = false; 
	private boolean isGetLocalUserID = false;
	public void pLogin() {
		//Log.e("BA", "pScrWelcom -> pLogin");
		if (!isGetLocalUserID) {
			// 获取本地登录是否成功
			isFinishLogin = ctrl_user_info.pIfLocalLogin();
			isGetLocalUserID = true;
		}
		if (!isFinishLogin) {
			String phone_number = ctrl_user_info.pGetLocalCellNumber();
			int user_id = ctrl_user_info.pCheckNumOnServer(phone_number);
			Log.e("pScrWelcome", "after checkNumOnServer, user_id = " + user_id);
			// 说明user_id不在服务器上
			if (user_id == 0) {
				Log.e("pScrWelcome", "reg, phonenum = phone_number = " + phone_number);
				user_id = ctrl_user_info.pUserReg(phone_number);
				//pro_dialog = null;
				//pro_dialog = ProgressDialog.show(pScrWelcome_ImageView.this,"注册中...", "您尚未注册，为您自动注册中..请稍后....", true, true);
			}
			ctrl_user_info.pUserLogin();
			isFinishLogin = true;
		} 
		
		// 等待动画完成之后再发送 转到下一个Activity的Handler消息
		if (isFinishLogin && !handler_thread.getIsRun()) {
			PushService.actionStart(context);
			Log.e(TAG, "PushService.actionStart(context)");
			my_handler.sendEmptyMessage(Constants.FINISH_LOGIN);
			loginThread.setIsRun(false);
		}
	}

	// 跳转至主界面 - pCB01F04
	public void pGotoMain() {
		Log.e("BA", "pScrWelcome -> pGotoMain");
		//if (ctrl_user_info.isFinishLogin) {
			Intent intent = new Intent();
			//intent.setClass(pScrWelcome_ImageView.this, mainActivity.class);
			intent.setClass(pScrWelcome_ImageView.this, pScrMain.class);
			this.startActivity(intent);
			// 取消进度对话框
			//pro_dialog.dismiss();
			// 跳转到主界面，停止线程
			//handler_thread.setIsRun(false);
		//}
	}
	
	@Override
	public boolean onTouch(View v, MotionEvent e) {
		// TODO Auto-generated method stub
		//Log.e("BA", "TOUCH");
		if (v.getId() == R.id.flipper_comp) {
			// 跳转到主界面
			switch(e.getAction()) {
			case MotionEvent.ACTION_DOWN:
				//Log.e("BA", "跳转到登录界面");
				break;
			}
		}
		return true;
	}
	
	 public void fadeIn() {
	    alpha += per_alpha;
	    if (alpha >255) {alpha = 255;}
	    image_view.setAlpha(alpha);
	    //Log.e("BA", "Fade in, alpha = " + alpha);
	 }
	 
	 public void fadeOut() {
		 alpha -= per_alpha;
		 if (alpha < 0) {alpha = 0;}
		 image_view.setAlpha(alpha);
		 //Log.e("BA", "Fade out, alpha = " + alpha);
	 }
}
